package task.manager;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * Activity class for creating an account.
 * 
 * @author Harrison Saylor, Aaron Mead, Marc Huet, Greg Ruskin
 * @version 2
 */
public class CreateAccountActivity extends Activity{
	
	private LoginFacade loginFacade;
	
	private EditText username;
	private EditText password1;
	private EditText password2;
	private Button submitBtn;
	private Button backBtn;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_account_activity);
		
		loginFacade = LoginFacade.getInstance();
		
		username = (EditText)findViewById(R.id.editText1);
		password1 = (EditText)findViewById(R.id.editText2);
		password2 = (EditText)findViewById(R.id.editText3);
		submitBtn = (Button)findViewById(R.id.button1);
		backBtn = (Button)findViewById(R.id.cancel2);
		
		submitBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				//System.out.println("Submit clicked");
				String currentUsername = username.getText().toString();
				String p1 = password1.getText().toString();
				String p2 = password2.getText().toString();
				
				if(currentUsername.length() > 0){
					if(p1.equals(p2)){
						//System.out.println("Adding entry");
						loginFacade.put(currentUsername,p1);
						finish();
					}
				}
			}
		});
		
		backBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent nextScreen = new Intent(getApplicationContext(),
						TaskManager2Activity.class);
				startActivity(nextScreen);
				finish();
				
			}
		});
	}
	
	@Override
	public void onPause(){
		super.onPause();
		
		try {
			loginFacade.saveLoginData();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

}
